package cn.rongdao.jrkabao.ui.information.adapter;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frwidget.switchbutton.CustomSwitch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.information.basicinfo.BasicInfoBean;
import cn.rongdao.jrkabao.util.listener.OnTextChangeListener;


/**
 * Created by jiangyongxing on 2016/7/21.
 * 描述：
 */
public class BasicInfoRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int ITEM_ADDRESS_LIST = 1;//标识为联系人的条目
    private static final int ITEM_WRITE_WITH_MONEY = 2;//标识为填写的是金额的条目
    private static final int ITEM_WRITE_WITHOUT_MONEY = 3;//标识为填写的不是金额的条目
    private static final int ITEM_SELECT_DATA = 4;//标识的是选择的条目
    private static final int ITEM_SELECT_DATA_EMERGENCY_CONTACT = 5;//标识的是选择紧急联系人的条目
    private static final int ITEM_SELECT_DATA_CITY = 6;//标识的是选择紧急联系人的条目
    private static final int ITEM_SELECT_WORK_PHONE = 7;//标识的是选择紧急联系人的条目

    private static final int NO_WRITE = 0;
    private static final int OTHERS = 1;

    private Activity mContext;
    private List<BasicInfoBean> list;
    private OnItemActionListener mOnItemActionListener;
    private HashMap<String, String> hashMap = new HashMap<>();
    private int mMaritalDefaultStatus;//表示记录用户是否填写过婚姻状况的状态
    private String mMaritalSelectedStatus;//表示记录用户所填写的婚姻状态的状态


    public BasicInfoRecyclerViewAdapter(Activity context, List<BasicInfoBean> list) {
        this.mContext = context;
        this.list = list;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case ITEM_ADDRESS_LIST:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_basic_info_address_list, parent, false);
                holder = new BasicInfoAddressListViewHolder(view);
                break;
            case ITEM_WRITE_WITH_MONEY:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_basic_info_write_with_money, parent, false);
                holder = new BasicInfoWithMoneyViewHolder(view);
                break;
            case ITEM_WRITE_WITHOUT_MONEY:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_basic_info_write_without_money, parent, false);
                holder = new BasicInfoWirteWithoutMoneyViewHolder(view);
                break;
            case ITEM_SELECT_DATA:
            case ITEM_SELECT_DATA_EMERGENCY_CONTACT:
            case ITEM_SELECT_DATA_CITY:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_basic_info_select_data, parent, false);
                holder = new BasicInfoSelectDataViewHolder(view);
                break;
            case ITEM_SELECT_WORK_PHONE:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_basic_info_company_phone, parent, false);
                holder = new BasicInfoCompanyPhoneViewHolder(view);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final BasicInfoBean basicInfoBean = list.get(position);
        switch (getItemViewType(position)) {
            case ITEM_ADDRESS_LIST:
                ((BasicInfoAddressListViewHolder) holder).itemName.setText(basicInfoBean.getShow_tag_cn());
                ((BasicInfoAddressListViewHolder) holder).itemSwitch.setState(basicInfoBean.getValue().equals("1"));
                ((BasicInfoAddressListViewHolder) holder).itemSwitch.setOnCheckedChangeListener(new CustomSwitch.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CustomSwitch customeSwitch, boolean checked) {
                        if (mOnItemActionListener != null)
                            mOnItemActionListener.onCheckedChangeListener(position, ((BasicInfoAddressListViewHolder) holder).itemSwitch, checked);
                    }
                });
                break;
            case ITEM_WRITE_WITH_MONEY:
                ((BasicInfoWithMoneyViewHolder) holder).itemName.setText(basicInfoBean.getShow_tag_cn());
                if (TextUtils.isEmpty(basicInfoBean.getValue())) {
                    ((BasicInfoWithMoneyViewHolder) holder).itemEt.setHint(basicInfoBean.getTip());
                } else {
                    ((BasicInfoWithMoneyViewHolder) holder).itemEt.setText(basicInfoBean.getValue());
                }
                ((BasicInfoWithMoneyViewHolder) holder).view.setVisibility(basicInfoBean.isHead() ? View.VISIBLE : View.GONE);
                ((BasicInfoWithMoneyViewHolder) holder).itemEt.addTextChangedListener(new OnTextChangeListener() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        if (mOnItemActionListener != null) {
                            String editTextStr = ((BasicInfoWithMoneyViewHolder) holder).itemEt.getText().toString().trim();
                            list.get(position).setValue(editTextStr);
                            mOnItemActionListener.onEdittextChangeListener(position, basicInfoBean.getShow_tag_field(), editTextStr);
                        }
                    }
                });
                break;
            case ITEM_WRITE_WITHOUT_MONEY:
                ((BasicInfoWirteWithoutMoneyViewHolder) holder).itemName.setText(basicInfoBean.getShow_tag_cn());
                if (TextUtils.isEmpty(basicInfoBean.getValue())) {
                    ((BasicInfoWirteWithoutMoneyViewHolder) holder).itemEt.setHint(basicInfoBean.getTip());
                } else {
                    ((BasicInfoWirteWithoutMoneyViewHolder) holder).itemEt.setText(basicInfoBean.getValue());
                }
                ((BasicInfoWirteWithoutMoneyViewHolder) holder).view.setVisibility(basicInfoBean.isHead() ? View.VISIBLE : View.GONE);
                ((BasicInfoWirteWithoutMoneyViewHolder) holder).itemEt.addTextChangedListener(new OnTextChangeListener() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        if (mOnItemActionListener != null) {
                            String editTextStr = ((BasicInfoWirteWithoutMoneyViewHolder) holder).itemEt.getText().toString().trim();
                            list.get(position).setValue(editTextStr);
                            mOnItemActionListener.onEdittextChangeListener(position, basicInfoBean.getShow_tag_field(), editTextStr);
                        }
                    }
                });
                break;
            case ITEM_SELECT_DATA:
                ((BasicInfoSelectDataViewHolder) holder).itemName.setText(basicInfoBean.getShow_tag_cn());
                ((BasicInfoSelectDataViewHolder) holder).itemContent.setTextColor(ContextCompat.getColor(mContext, TextUtils.isEmpty(basicInfoBean.getShow_tag()) ? R.color.c174180194 : R.color.c515151));
                ((BasicInfoSelectDataViewHolder) holder).itemContent.setText(TextUtils.isEmpty(basicInfoBean.getShow_tag()) ? basicInfoBean.getTip() : basicInfoBean.getShow_tag());
                ((BasicInfoSelectDataViewHolder) holder).view.setVisibility(basicInfoBean.isHead() ? View.VISIBLE : View.GONE);

                if ("marital_status".equalsIgnoreCase(basicInfoBean.getShow_tag_field())) {
                    judgmentOfMaritalStatus(basicInfoBean.getShow_tag());
                    mMaritalSelectedStatus = basicInfoBean.getMarital_status();
                }
                ((BasicInfoSelectDataViewHolder) holder).itemLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemActionListener != null) {
                            mOnItemActionListener.onSelectDataClicklistener(position, basicInfoBean.getSelect_data(), basicInfoBean.getShow_tag_cn());
                        }
                    }
                });
                break;
            case ITEM_SELECT_DATA_EMERGENCY_CONTACT:
                ((BasicInfoSelectDataViewHolder) holder).itemName.setText(basicInfoBean.getShow_tag_cn());
                ((BasicInfoSelectDataViewHolder) holder).itemContent.setTextColor(ContextCompat.getColor(mContext, TextUtils.isEmpty(basicInfoBean.getValue()) ? R.color.c174180194 : R.color.c515151));
                ((BasicInfoSelectDataViewHolder) holder).itemContent.setText(TextUtils.isEmpty(basicInfoBean.getShow_tag()) ? basicInfoBean.getTip() : TextUtils.concat(basicInfoBean.getShow_tag(), "/", basicInfoBean.getValue_1(), "/", basicInfoBean.getValue_2()));
                //因为有两种保存的方式，所以这里要做特别的处理
                try {
                    ((BasicInfoSelectDataViewHolder) holder).itemContent.setText(basicInfoBean.getValue().equals("") ? list.get(position).getTip() : list.get(position).getSelect_data().get(Integer.parseInt(basicInfoBean.getValue())) + "/" + list.get(position).getValue_1() + "/" + list.get(position).getValue_2());
                } catch (Exception w) {
                    ((BasicInfoSelectDataViewHolder) holder).itemContent.setText(basicInfoBean.getValue().equals("") ? list.get(position).getTip() : list.get(position).getShow_tag() + "/" + list.get(position).getValue_1() + "/" + list.get(position).getValue_2());
                }
                ((BasicInfoSelectDataViewHolder) holder).view.setVisibility(basicInfoBean.isHead() ? View.VISIBLE : View.GONE);
                ((BasicInfoSelectDataViewHolder) holder).itemLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemActionListener != null) {
                            mOnItemActionListener.onemergencyContactClickListener(position, basicInfoBean.getShow_tag_cn(), basicInfoBean.getValue(), basicInfoBean.getValue_1(), basicInfoBean.getValue_2(), basicInfoBean.getSelect_data());
                        }
                    }
                });
                break;
            case ITEM_SELECT_DATA_CITY:
                ((BasicInfoSelectDataViewHolder) holder).itemName.setText(basicInfoBean.getShow_tag_cn());
                ((BasicInfoSelectDataViewHolder) holder).itemContent.setTextColor(ContextCompat.getColor(mContext, TextUtils.isEmpty(basicInfoBean.getValue()) ? R.color.c174180194 : R.color.c515151));
                ((BasicInfoSelectDataViewHolder) holder).itemContent.setText(TextUtils.isEmpty(basicInfoBean.getValue()) ? basicInfoBean.getTip() : basicInfoBean.getValue());
                ((BasicInfoSelectDataViewHolder) holder).view.setVisibility(basicInfoBean.isHead() ? View.VISIBLE : View.GONE);
                ((BasicInfoSelectDataViewHolder) holder).itemLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemActionListener != null) {
                            mOnItemActionListener.onCityItemClickListener(position);
                        }
                    }
                });
                break;
            case ITEM_SELECT_WORK_PHONE:
                ((BasicInfoCompanyPhoneViewHolder) holder).itemName.setText(basicInfoBean.getShow_tag_cn());
                String value = basicInfoBean.getValue();
                if (!TextUtils.isEmpty(value)) {
                    if (value.contains("/")) {
                        String[] phones = value.split("/");
                        if (!(phones.length > 2)) {
                            ((BasicInfoCompanyPhoneViewHolder) holder).itemPhone.setText(phones[1]);
                            ((BasicInfoCompanyPhoneViewHolder) holder).itemQuhao.setText(phones[0]);
                        }
                    } else {
                        if (value.length() > 4) {
                            ((BasicInfoCompanyPhoneViewHolder) holder).itemPhone.setText(value.substring(0, 4));
                            ((BasicInfoCompanyPhoneViewHolder) holder).itemQuhao.setText(value.substring(4, value.length()));
                        }
                    }
                }
                ((BasicInfoCompanyPhoneViewHolder) holder).itemPhone.addTextChangedListener(new OnTextChangeListener() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        if (mOnItemActionListener != null) {
                            mOnItemActionListener.onEdittextChangeListener(position, basicInfoBean.getShow_tag_field(),
                                    (String) TextUtils.concat(((BasicInfoCompanyPhoneViewHolder) holder).itemQuhao.getText().toString().trim(), "/", ((BasicInfoCompanyPhoneViewHolder) holder).itemPhone.getText().toString().trim()));
                        }
                    }
                });
                ((BasicInfoCompanyPhoneViewHolder) holder).itemQuhao.addTextChangedListener(new OnTextChangeListener() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        if (mOnItemActionListener != null) {
                            mOnItemActionListener.onEdittextChangeListener(position, basicInfoBean.getShow_tag_field(),
                                    (String) TextUtils.concat(((BasicInfoCompanyPhoneViewHolder) holder).itemQuhao.getText().toString().trim(), "/", ((BasicInfoCompanyPhoneViewHolder) holder).itemPhone.getText().toString().trim()));
                        }
                    }
                });
                break;
        }
    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public void setOnItemActionListener(OnItemActionListener onItemActionListener) {
        this.mOnItemActionListener = onItemActionListener;
    }


    @Override
    public int getItemViewType(int position) {
        switch (list.get(position).getType()) {
            case 0:
                if ("month_income".equalsIgnoreCase(list.get(position).getShow_tag_field()) || "after_tax_income".equalsIgnoreCase(list.get(position).getShow_tag_field())) {//需要带元的条目
                    return ITEM_WRITE_WITH_MONEY;
                }
                if ("work_place".equalsIgnoreCase(list.get(position).getShow_tag_field()) || "city".equalsIgnoreCase(list.get(position).getShow_tag_field()))
                    return ITEM_SELECT_DATA_CITY;
                if ("work_phone".equalsIgnoreCase(list.get(position).getShow_tag_field()))
                    return ITEM_SELECT_WORK_PHONE;
                return ITEM_WRITE_WITHOUT_MONEY;//不需要带元的条目
            case 1:
                return ITEM_SELECT_DATA;//选择数据的条目
            case 2:
                return ITEM_SELECT_DATA_EMERGENCY_CONTACT;//紧急联系人的条目
            case 3:
                return ITEM_ADDRESS_LIST;//通讯录
        }

        return super.getItemViewType(position);
    }

    public HashMap<String, String> getHashMap() {
        return hashMap;
    }

    public int getMaritalDefaultStatus() {
        return mMaritalDefaultStatus;
    }

    public String getMaritalSelectedStatus() {
        return mMaritalSelectedStatus;
    }

    public String getVaule() {
        if ("authorized_contact".equals(list.get(0).getShow_tag_field())) {
            return list.get(0).getValue();
        } else return "";
    }


    private void judgmentOfMaritalStatus(String mMaritalStatus) {
        switch (mMaritalStatus) {
            case "":
                //没有填写
                mMaritalDefaultStatus = NO_WRITE;
                break;
            default:
                mMaritalDefaultStatus = OTHERS;
                break;

        }
    }


    /**********
     * 定义点击事件
     **********/
    public interface OnItemActionListener {

        void onSelectDataClicklistener(int position, ArrayList<String> selectData, String selectDataTitle);

        void onCityItemClickListener(int position);

        void onemergencyContactClickListener(int position, String title, String relation, String name, String number, ArrayList<String> selectData);

        void onCheckedChangeListener(int position, CustomSwitch itemSwitch, boolean isChecked);

        void onEdittextChangeListener(int position, String tagCn, String content);

    }

    class BasicInfoAddressListViewHolder extends RecyclerView.ViewHolder {
        public TextView itemName;
        public CustomSwitch itemSwitch;


        public BasicInfoAddressListViewHolder(View layout) {
            super(layout);
            itemName = (TextView) layout.findViewById(R.id.item_basic_info_name);
            itemSwitch = (CustomSwitch) layout.findViewById(R.id.item_basic_info_switch);

        }
    }

    class BasicInfoWithMoneyViewHolder extends RecyclerView.ViewHolder {
        public TextView itemName;
        public TextView itemYuan;
        public EditText itemEt;
        public LinearLayout itemLL;
        public View view;

        public BasicInfoWithMoneyViewHolder(View layout) {
            super(layout);
            itemName = (TextView) layout.findViewById(R.id.item_basic_info_name);
            itemYuan = (TextView) layout.findViewById(R.id.item_basic_info_yuan);
            itemEt = (EditText) layout.findViewById(R.id.item_basic_info_content2);
            itemLL = (LinearLayout) layout.findViewById(R.id.item_basic_info_write_with_money_ll);
            view = layout.findViewById(R.id.item_basic_info_view);
        }
    }

    class BasicInfoWirteWithoutMoneyViewHolder extends RecyclerView.ViewHolder {

        public TextView itemName;
        public EditText itemEt;
        public LinearLayout itemLL;
        public View view;

        public BasicInfoWirteWithoutMoneyViewHolder(View layout) {
            super(layout);
            itemName = (TextView) layout.findViewById(R.id.item_basic_info_name);
            itemEt = (EditText) layout.findViewById(R.id.item_basic_info_content2);
            itemLL = (LinearLayout) layout.findViewById(R.id.item_basic_info_write_without_money_ll);
            view = layout.findViewById(R.id.item_basic_info_view);
        }
    }

    class BasicInfoSelectDataViewHolder extends RecyclerView.ViewHolder {
        public TextView itemName;
        public TextView itemContent;
        public LinearLayout itemLL;
        public View view;

        public BasicInfoSelectDataViewHolder(View layout) {
            super(layout);
            itemName = (TextView) layout.findViewById(R.id.item_basic_info_name);
            itemContent = (TextView) layout.findViewById(R.id.item_basic_info_content1);
            itemLL = (LinearLayout) layout.findViewById(R.id.item_basic_info_select_data_ll);
            view = layout.findViewById(R.id.item_basic_info_view);
        }
    }

    class BasicInfoCompanyPhoneViewHolder extends RecyclerView.ViewHolder {
        public TextView itemName;
        public EditText itemQuhao;
        public EditText itemPhone;
        public View view;

        public BasicInfoCompanyPhoneViewHolder(View layout) {
            super(layout);
            itemName = (TextView) layout.findViewById(R.id.item_basic_info_name);
            itemQuhao = (EditText) layout.findViewById(R.id.item_basic_info_quhao);
            itemPhone = (EditText) layout.findViewById(R.id.item_basic_info_phone);
            view = layout.findViewById(R.id.item_basic_info_view);
        }
    }

}
